import React, { Component } from 'react';
import { InputNumber } from 'antd';
import { maxLength } from '@/utils/form';

function CusInputNumber(props) {
  const handleKeyUp = e => {
    e.target.value = e.target.value.replace(/[^\d.]/g, '');
    e.target.value = e.target.value.replace(/\.{2,}/g, '.');
    e.target.value = e.target.value
      .replace('.', '$#$')
      .replace(/\./g, '')
      .replace('$#$', '.');
  };

  return (
    <div className="ant-input-group-wrapper">
      <div className="ant-input-wrapper ant-input-group">
        <InputNumber
          onKeyUp={handleKeyUp}
          style={{ width: '100%' }}
          {...props}
          onInput={e => maxLength(e, 5)}
        />
        {props.addonAfter && <span className="ant-input-group-addon">{props.addonAfter}</span>}
      </div>
    </div>
  );
}

export default CusInputNumber;
